/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#ifndef PROFILECONFIG_H
#define PROFILECONFIG_H

#include "MouseControl.h"

//-- Define the conifg file names.
#define CONFIGFILE_NAME "config.ini"
#define CONFIGFILE_LOCAL_NAME "/cocon.ini"
#define CONFIGFILE_ZIP_NAME "/cocon_zip.ini"
#define KEYFILE_NAME "key.map"
#define GESTUREFILE_NAME "gesture.map"
#define BOOKMARK_NAME "bookmark.txt"

/**
 * Operations of main window.
 */
enum MainOperType
{
    Oper_Invalid,
    Oper_PrevPage,
    Oper_NextPage,
    Oper_Close,
    Oper_Grayscale,
    Oper_Menu,
    Oper_StatusDlg,
    Oper_FileBrowser,
    Oper_Origin,
    Oper_GotoPage,
    Oper_ScrollUp,
    Oper_ScrollDown,
    Oper_ScrollLeft,
    Oper_ScrollRight,
    Oper_MoveUp,
    Oper_MoveDown,
    Oper_MoveLeft,
    Oper_MoveRight,
    Oper_Drag,
    Oper_Refresh,
    Oper_PrevComic,
    Oper_NextComic,
    Oper_AddBookmark,
    Oper_RemoveBookmark,
    Oper_ManageBookmark,
};

/**
 * The options closely related to image processing.
 */
enum ImageOption
{
    Opt_None        = 0x00000000,

    //-- display effect
    Opt_DividePage      = 0x00000001,
    Opt_RightToLeft     = 0x00000002,
    Opt_Align           = 0x00000004, 
    Opt_CutMargin       = 0x00000008,

    //-- display mode
    Opt_SideWards       = 0x00000100,
    Opt_FitToWidth      = 0x00000200,
    Opt_FitToHeight     = 0x00000400,
    Opt_AutoNavigate    = 0x00000800,
    Opt_ScrollMode      = 0x00001000,

    Opt_End = 0xffffffff
};

/**
 * The struct described arguments for image processing. 
 * If values of it's members change, processing whould need to redo.
 * 
 * @author reach (5/25/2010)
 */
struct ImageProfile
{
    int option;
    int maxMarginPercent; // the maximum size margin can be cut, described by the ratio of the original image size 
    int ignoreSize; // the size in pixel used to decide whether a block need to be ignore
    float minDegree; // the minimal degree need to correct

    bool operator!=(const ImageProfile& profile)
    {
        return(option != profile.option || 
               maxMarginPercent != profile.maxMarginPercent ||
               ignoreSize != profile.ignoreSize ||
               minDegree != profile.minDegree);
    }

    bool operator==(const ImageProfile& profile)
    {
        return(option == profile.option &&
               maxMarginPercent == profile.maxMarginPercent &&
               ignoreSize == profile.ignoreSize &&
               minDegree == profile.minDegree);
    }
};

/**
 * maps of keys and gestures to main operations
 */
typedef QMap<int, int> CoKeyMap;  
typedef QMap<int, int> CoGestureMap;

/**
 * arguments of grayscale setting
 * 
 * @author reach (5/25/2010)
 */
struct GrayScaleSet
{
    int black; // black field
    float middle; // gamma value
    int white; // white field
};

/**
 * Datas for bookmark.
 * 
 * @author reach (5/25/2010)
 */
struct Bookmark
{
    QString path;
    QString date;
};
typedef QList<Bookmark> Bookmarks;

/**
 * The profile include all arguments describe which comic to load and how process and display it.
 * 
 * @author reach (5/25/2010)
 */
class CoconProfile
{
    //-- public members
public:
    ImageProfile imgProfile;
    QString filePath;
    int iPage;
    int iDivPage;
    int iFlushMode; // 1: part flush  0: clear flush
    int iClearInterval; // the interval times between clear update 
    int iSideWardsDirect; // 0: left-hand  1: right-hand
    int iMoveDistance;
    int iScrollMark; // whether display the mark next scroll down turn to
    GrayScaleSet grayscale;
    CoKeyMap keyMap;
    CoGestureMap gestureMap;
    Bookmarks bookmarks;
    bool isZip;

    //-- constructor
public:
    CoconProfile();

    //-- public methods
public:
    void readProfile();
    void writeProfile();
    void readConfig(const QString filename = CONFIGFILE_NAME);
    void writeConfig(const QString filename = CONFIGFILE_NAME);
    void readZipConfig();
    void writeZipConfig();
    void readLocalConfig();
    void writeLocalConfig();
    void readBookmarks();
    void writeBookmarks();
    void initKeyMap();
    void initGestureMap();
    void initProfile();
    void addBookmark();
    void removeBookmark(int id);
    QString toString();

    //-- private enum
private:
    enum ConfigItem
    {
        Cf_Option,
        Cf_MaxMargin,
        Cf_IgnoreSize,
        Cf_MinDegree,
        Cf_Grayscale,
        Cf_FileDir,
        Cf_PageNum,
        Cf_FlushMode,
        Cf_ClearInterval,
        Cf_SideWardsDirect,
        Cf_MoveDistance,
        Cf_ScrollMark,
        Cf_Date,
    };

    //-- private members
private:
    QMap<QString, int> m_configMap;
};

#endif // PROFILECONFIG_H
